package app.fugema.shop.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.base.common.model.bean.QQLoginBean;
import com.base.common.utils.UIUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.tencent.connect.common.Constants;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.helper.QQHelper;
import app.fugema.shop.helper.WeChatHelper;

public class WXEntryActivity1 extends Activity implements IWXAPIEventHandler
{
    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;
    private static final int RETURN_MSG_TYPE_PAY = 5;
    private static String TAG = "WXEntryActivity";

    private boolean isQQLogin = false;
    private IUiListener iUiListener;//qq登陆

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isQQLogin = getIntent().getBooleanExtra("isQQLogin", false);
        if (isQQLogin) {
            if (QQHelper.isQQAvilible(this)) {
                iUiListener = new QQLoginListener(WXEntryActivity1.this);
                /*
                new IUiListener() {
                    @Override
                    public void onComplete(Object o) {
                        try {
                            String openid = ((JSONObject) o).getString("openid");
                            String accessToken = ((JSONObject) o).getString("access_token");
                            String expiresIn = ((JSONObject) o).getString("expires_in");
                            QQLoginBean loginBean = new QQLoginBean();
                            loginBean.setOpenid(openid);
                            loginBean.setAccess_token(accessToken);
                            loginBean.setExpires_in(expiresIn);
                            LiveEventBus.get(AppConstants.QQ_LOGIN, QQLoginBean.class).post(loginBean);
                            finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(UiError uiError) {
                        UIUtils.showToastSafes(uiError.errorCode + "\r\n" + uiError.errorMessage + "\r\n" + uiError.errorDetail);
                        finish();
                    }
                    @Override
                    public void onCancel() {
                        finish();
                    }
                };
                */
                QQHelper.newInstance().login(this, iUiListener);
            } else {
                UIUtils.showToastSafes("没有安装QQ");
                finish();
            }
        } else {
            WeChatHelper.init(this);
            WeChatHelper.handResp(getIntent(), this);
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (isQQLogin) {
            if (requestCode == Constants.REQUEST_LOGIN) {
                if (iUiListener != null)
                    Tencent.onActivityResultData(requestCode, resultCode, data, iUiListener);
            }
        } else {
            setIntent(data);
            WeChatHelper.handResp(getIntent(), this);
        }

    }

    class QQLoginListener implements IUiListener
    {
        private Activity mAty;

        public QQLoginListener(Activity activity)
        {
            this.mAty = activity;
        }

        @Override
        public void onComplete(Object o) {
            try {
                String openid = ((JSONObject) o).getString("openid");
                String accessToken = ((JSONObject) o).getString("access_token");
                String expiresIn = ((JSONObject) o).getString("expires_in");
                QQLoginBean loginBean = new QQLoginBean();
                loginBean.setOpenid(openid);
                loginBean.setAccess_token(accessToken);
                loginBean.setExpires_in(expiresIn);
                LiveEventBus.get(AppConstants.QQ_LOGIN, QQLoginBean.class).post(loginBean);
                this.mAty.finish();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(UiError uiError)
        {
            UIUtils.showToastSafes(uiError.errorCode + "\r\n" + uiError.errorMessage + "\r\n" + uiError.errorDetail);
            this.mAty.finish();
        }

        @Override
        public void onCancel()
        {
            this.mAty.finish();
        }

        @Override
        public void onWarning(int i)
        {

        }
    }


    @Override
    public void onResp(BaseResp baseResp) {
        Log.e(TAG, baseResp.errStr + baseResp.errCode + baseResp.openId + baseResp.transaction);
        // 1.登录  2.分享 5.支付
        int type = baseResp.getType();
        String result = WeChatHelper.handResp2Str(baseResp);
        if (!TextUtils.isEmpty(result)) {
            UIUtils.showToastSafes(result);
        } else {
//            getRespByType(type);
            // 将传递给Login
            if (type == RETURN_MSG_TYPE_LOGIN) {
                // 登录成功
                SendAuth.Resp loginResp = (SendAuth.Resp) baseResp;
                LiveEventBus.get(AppConstants.WECHAT_LOGIN).post(loginResp.code);
            } else if (type == RETURN_MSG_TYPE_SHARE) {
                // 分享成功
                LiveEventBus.get(AppConstants.WECHAT_SHARE).post(0);
            }

        }
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    private void getRespByType(int type) {
        switch (type) {
            case RETURN_MSG_TYPE_LOGIN:
                break;
            case RETURN_MSG_TYPE_SHARE:
                break;
            case RETURN_MSG_TYPE_PAY:
                break;
            default:
                break;
        }

    }
}
